﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Data.Linq;
using System.Drawing;
using Website.Logger;
using DAL;
using BOL;
using System.Web.Script.Services;
using System.Web.Services;
using UtilityExtensions;


namespace UILGestion.ObraAdmin.RendicionCaja
{
    public partial class RendicionDepositoGrid : System.Web.UI.UserControl
    {

        public RendicionDepositoGrid()

        {
            item = new RendicionBL();
        }
        private RendicionBL item;

       

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Load(string idPadre,int idestado,int idObra)
        {
            idParent.Value = idPadre;
            idEstado.Value = idestado.ToString();
            this.idObra.Value = idObra.ToString();
            BindDataItem();
        }

        public void updateTotalesview()
        {

            var a = ((UILGestion.ObraAdmin.RendicionCaja.RendicioneditForm)this.Page);
            a.setestadistica();

            

        }

        protected void DeleteItem(object sender, EventArgs e)
        {
            try
            {

                ImageButton lnkRemove = (ImageButton)sender;
                int idInternal = int.Parse(lnkRemove.CommandArgument.ToString());

               item.DeleteDeposito( idInternal);
               updateTotalesview();
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void AddItem(object sender, EventArgs e)
        {
            try
            {
                Button lnkdivrecurso = (Button)sender;

                TextBox fechaTxt =
                    ((TextBox)itemGridView.FooterRow.FindControl("fechaTxt"));
                DropDownList tipodocumentoDDL =
                    ((DropDownList)itemGridView.FooterRow.FindControl("formaEntregaDDL"));
                TextBox nroDocumentoTxt =
                    ((TextBox)itemGridView.FooterRow.FindControl("nroDocumentoTxt"));
                TextBox descripcionTxt =
                    ((TextBox)itemGridView.FooterRow.FindControl("descripcionTxt"));

                
                TextBox conceptoTxt =
                    ((TextBox)itemGridView.FooterRow.FindControl("conceptoTxt"));
                TextBox totalTxt =
                    ((TextBox)itemGridView.FooterRow.FindControl("totalTxt"));


                using (dbGestionDataContext _db= new dbGestionDataContext(Parametros.strConnString))
                {
                    
               
                item.AddDeposito(    _db ,
                                idParent.Value.ToInt(),
                                fechaTxt.Text.ToDate().Value,
                                tipodocumentoDDL.SelectedValue.ToInt(),
                                nroDocumentoTxt.Text,
                                totalTxt.Text.ToDecimal().Value);

                  }
                updateTotalesview();
                BindDataItem();
                // GridUpdatePanel.Update();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void UpdateItem2(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                

                GridView gv = ((GridView)sender);

                TextBox fechaTxt =
                   ((TextBox)gv.Rows[e.RowIndex].FindControl("fechaTxt"));
                DropDownList tipodocumentoDDL =
                    ((DropDownList)gv.Rows[e.RowIndex].FindControl("formaEntregaDDL"));
                TextBox nroDocumentoTxt =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("nroDocumentoTxt"));
               
                TextBox totalTxt =
                    ((TextBox)itemGridView.FooterRow.FindControl("totalTxt"));


                int idinternal = int.Parse(gv.DataKeys[e.RowIndex].Values[0].ToString());



                item.UpdateDeposito(idinternal,fechaTxt.Text.ToDate().Value,
                                        tipodocumentoDDL.SelectedValue.ToInt(),
                                        nroDocumentoTxt.Text,
                                       totalTxt.Text.ToDecimal().Value);
                                        
                gv.EditIndex = -1;
                itemGridView.FooterRow.Visible = true;
                updateTotalesview();
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void EditItem2(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);
                gv.EditIndex = e.NewEditIndex;
                BindDataItem();
                gv.FooterRow.Visible = false;
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        public void BindDataItem()
        {
           
            itemGridView.DataSource = item.getRecepcionDepositoList(idParent.Value.ToInt()).ToList();
            itemGridView.ShowFooter = false;
            try
            {
                item.HabilitadoToDeposito(DbUtil.Db, idObra.Value.ToInt());
                itemGridView.ShowFooter = true;
            }
            catch { }


            if (idEstado.Value.ToInt() != 39)  //abierto
            {
                
                
                itemGridView.ShowFooter = false;
                itemGridView.Columns[4].Visible = false;
                itemGridView.DataBind();
            }
            else
            {
                itemGridView.Columns[4].Visible = true;
                itemGridView.DataBind();
                resetEmpty(itemGridView);
                
                setFooter();
            }

        }
        private void setFooter()
        {
            DropDownList tipodocumentoDDL =
                ((DropDownList)itemGridView.FooterRow.FindControl("formaEntregaDDL"));

            
            tipodocumentoDDL.DataTextField = "descripcion";
            tipodocumentoDDL.DataValueField = "idInternal";
            tipodocumentoDDL.DataSource = new ListValueBL().GetFormaEntrega();
            tipodocumentoDDL.DataBind();
        }
        protected void CancelItem2(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);

                gv.EditIndex = -1;
                gv.FooterRow.Visible = true;
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        private void resetEmpty(GridView gv)
        {
            if (gv.Rows.Count == 0)
            {
                List<RendicionDepositoInfo> itemList0 = new List<RendicionDepositoInfo>();
                RendicionDepositoInfo itemcero = new RendicionDepositoInfo();

                itemList0.Add(itemcero);
                gv.DataSource = itemList0;
                gv.DataBind();
                gv.Rows[0].Visible = false;
            }

        }
    }
}